package day9.guanlixitong.src.ui;

import day9.guanlixitong.src.bean.Employee;
import day9.guanlixitong.src.bean.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import day9.guanlixitong.src.ui.LoginFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//这个窗口是监听器但是你需要啊添加到按钮上面，这个对象是形参
public class RegisterFrame extends JFrame implements ActionListener {

    private JTextField nameField; // 姓名输入框
    private JTextField usernameField; // 用户名输入框
    private JPasswordField passwordField; // 密码输入框
    private JButton submitButton; // 提交按钮
    //private ArrayList<User> all_users=new ArrayList<>();

    public static void main(String[] args) {
        // 确保Swing组件在事件调度线程上创建
         new RegisterFrame();
    }

    public RegisterFrame() {
        setTitle("注册窗口");
        setSize(400, 280); // 设置窗口大小，稍微增加高度以适应新的输入框
        setLocationRelativeTo(null); // 居中显示
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置内容面板的布局为BorderLayout
        setLayout(new BorderLayout());

        // 创建一个面板来放置注册表单
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(4, 1, 10, 10)); // 4行1列布局
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 内边距
        formPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // 居中对齐

        // 添加姓名标签和文本框
        JLabel nameLabel = new JLabel("姓名:", SwingConstants.RIGHT);
        nameLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(nameLabel);

        nameField = new JTextField();
        nameField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(nameField);

        // 添加用户名标签和文本框
        JLabel userLabel = new JLabel("用户名:", SwingConstants.RIGHT);
        userLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(userLabel);

        usernameField = new JTextField();
        usernameField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(usernameField);

        // 添加密码标签和密码框
        JLabel passLabel = new JLabel("密码:", SwingConstants.RIGHT);
        passLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(passLabel);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("楷体", Font.PLAIN, 14));
        formPanel.add(passwordField);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // 居中对齐

        // 添加提交按钮
        submitButton = createStyledButton("提交");
        buttonPanel.add(submitButton);
        submitButton.addActionListener(this);
        //这个添加监听器对象的方法形参是监听器，那我需要提供一个监听器（一个类）

//        submitButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                register();
//            }
//        });


        // 将注册表单和按钮添加到主窗口
        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置背景颜色
        getContentPane().setBackground(new Color(240, 240, 240));

        // 设置可见性
        setVisible(true);
    }

    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("楷体", Font.BOLD, 14));
        button.setBackground(new Color(0, 128, 255));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        button.setPreferredSize(new Dimension(120, 30)); // 设置按钮大小
        return button;
    }

//    public void register() {
//        String name = nameField.getText();
//        String username = usernameField.getText();
//        String password = new String(passwordField.getPassword());
//
//        if (name.isEmpty() || username.isEmpty() || password.isEmpty()) {
//            JOptionPane.showMessageDialog(this, "姓名、用户名和密码不能为空！");
//        } else {
//            // 这里可以添加注册逻辑，例如验证用户名是否已存在、保存用户信息等
//            JOptionPane.showMessageDialog(this, "注册成功！\n姓名: " + name + "\n用户名: " + username + "\n密码: " + password);
//            // 清空输入框
//            nameField.setText("");
//            usernameField.setText("");
//            passwordField.setText("");
//        }
//    }

    @Override
       public void actionPerformed(ActionEvent e)//触发这个方法前提是我给某个组件添加了监听器，窗口是监听器你没有打开使用他
        {
            register(e);
        }
    public void register(ActionEvent e)
    {
        if (e.getSource() == submitButton) {
            String name = nameField.getText();
            String user_name = usernameField.getText();
            String password = new String(passwordField.getPassword());
            if (name.isEmpty() || user_name.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(this, "注册不规范不能为空");
            } else {
                LoginFrame.all_users.add(new User(name, user_name, password));//权限是缺省
                JOptionPane.showMessageDialog(this, "注册成功\n" + "姓名：" + name + "\n用户名：" + user_name);
                  this.dispose();
            }
        }
//        for(int i=0;i<LoginFrame.all_users.size();i++)
//        {
//            System.out.println(LoginFrame.all_users.get(i).getUsername());
//        }
//        确保我添加进去了

    }





    }
